package com.liu.io.file.pdf;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;

/**
 * PDF水印
 * @author liubo
 */
public class Watermark extends PdfPageEventHelper {

    /**
     * 水印内容
     */
    private String watermarkText;

    Font FONT = new Font(Font.COURIER, 12, Font.BOLD);

    public Watermark() {

    }

    public Watermark(String watermarkText) {
        this.watermarkText = watermarkText;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                ColumnText.showTextAligned(writer.getDirectContentUnder(),
                        Element.ALIGN_CENTER,
                        new Phrase(this.watermarkText == null ? "" : this.watermarkText, FONT),
                        (50.5f + i * 350),
                        (40.0f + j * 150),
                        writer.getPageNumber() % 2 == 1 ? 45 : -45);
            }
        }
    }
}

